package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.*;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.dto.MallMemberPayStandardDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by DK on 17/3/7.
 */
@Repository
public class MallMemberPayStandardRepositoryImpl implements MallMemberPayStandardRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MallMemberPayStandardRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public PageResult<MallMemberPayStandardDto> findMallMemberPayStandardPage(Long mallId, String mallName, String isDelete, PageResult page) {
        SQLPageDoing<MallMemberPayStandardDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from mall_member_pay_standard mp left join mall m on mp.mall_id = m.id left join user_admin ua on mp.operator_id = ua.user_id where 1=1 ");
            if (mallId != null) {
                sql.append(" and mp.mall_id = :mallId");
                params.put("mallId", mallId);
            }
            if (StringUtils.isNotBlank(mallName)) {
                sql.append(" and m.name LIKE :mallName");
                params.put("mallName", "%" + mallName + "%");
            }
            if(StringUtils.isNotBlank(isDelete)){
                sql.append(" and mp.is_delete = :isDelete");
                params.put("isDelete", isDelete);
            }
            sql.append(" and m.audit_certify_info_status = 'AUDIT_PASS'");
            sql.append(" order by mp.date_created desc");
            fields.append("mp.*, m.name mallName, ua.name operatorName");
        };
        return doing.go(page, MallMemberPayStandardDto.class, objectMapper, jdbcTemplate, log);
    }

    /**
     * 计算平台的商户数不包括本身
     * @param mallId
     * @return
     */
    @Override
    public List<Long> compulateMallMerchantsCount(Long mallId, String startDate, String endDate) {
        SQLListDoing<Long> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM merchant m where 1=1");
            sql.append(" and m.status='VERIFIED'");
            sql.append(" and m.mall_id=:mallId");
            sql.append(" and not exists(select 1 from mall ma where ma.id=:mallId and ma.account_id=m.account_id)");
            params.put("mallId", mallId);
            if (startDate != null) {
                sql.append(" AND m.date_created>=:startDate");
                params.put("startDate", startDate);
            }
            if (endDate != null) {
                sql.append(" AND m.date_created<=:endDate");
                params.put("endDate", endDate);
            }
            fields.append("m.id");
        };
        return doing.go(Long.class, jdbcTemplate, log, objectMapper);
    }

    /**
     * 计算平台的商户交易金额不包括本身
     * @param mallId
     * @param merchantIds
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public Double compulateMallMerchantsAmount(Long mallId, List<Long> merchantIds, String startDate, String endDate) {
        SQLObjectDoing<Double> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM merchant_order_verification mov");
            sql.append(" WHERE mov.is_delete='NO'");
            sql.append(" AND mov.merchant_role in('SELF','SUPPLIER')");
            sql.append(" AND mov.mall_id=:mallId");
            sql.append(" AND mov.merchant_id in(:merchantId)");
            params.put("mallId", mallId);
            params.put("merchantId", merchantIds);

            if (startDate != null) {
                sql.append(" AND mov.date_created>=:startDate");
                params.put("startDate", startDate);
            }
            if (endDate != null) {
                sql.append(" AND mov.date_created<=:endDate");
                params.put("endDate", endDate);
            }
            fields.append("sum(mov.verify_price)");
        };
        return doing.go(Double.class, jdbcTemplate, log, objectMapper);
    }
}
